package com.yeyks.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.yeyks.cnbooking.Result.addresssearch.CbAddressSearchData;
import com.yeyks.cnbooking.Result.hotellistsearch.CbHotelListSearchData;
import com.yeyks.cnbooking.Result.booking.CbBookingData;
import com.yeyks.cnbooking.Result.hotelsearch.CbHotelSearchData;
import com.yeyks.cnbooking.Result.ordersearch.CbOrderSearchData;
import com.yeyks.cnbooking.Result.prebookingcheck.CbPreBookingCheckData;
import com.yeyks.cnbooking.Result.rateplansearch.CbRatePlanSearchData;
import com.yeyks.cnbooking.Result.roomsearch.CbRoomSearchData;
import com.yeyks.cnbooking.base.CNResponse;
import com.yeyks.cnbooking.dto.hotel.HotelInfoDTO;
import com.yeyks.cnbooking.param.CbPreBookingCheckParam;
import com.yeyks.cnbooking.param.addresssearch.CbAddressSearchParam;
import com.yeyks.cnbooking.param.booking.CbBookInfo;
import com.yeyks.cnbooking.param.booking.CbBookingParam;
import com.yeyks.cnbooking.param.booking.CbGuestInfo;
import com.yeyks.cnbooking.param.booking.CbGuestInfos;
import com.yeyks.cnbooking.param.hotel.CbHotelListSearchParam;
import com.yeyks.cnbooking.param.hotel.CbHotelListSearchStayDateRange;
import com.yeyks.cnbooking.param.hotel.CbHotelSearchParam;
import com.yeyks.cnbooking.param.ordersearch.CbOrderSearchParam;
import com.yeyks.cnbooking.param.rateplansearch.CbRatePlanSearchParam;
import com.yeyks.cnbooking.param.roomsearch.CbRoomSearchParam;
import com.yeyks.cnbooking.service.CNBookingService;
import com.yeyks.cnbooking.service.TransitionService;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.bag.SynchronizedSortedBag;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: admin
 * @description:
 * @author: chenzheng
 * @create: 2019-09-26 16:59
 **/
@Api(tags = "CnBooking相关接口")
@RestController
@RequestMapping(value = "/cb")
public class CnBookingController {

    @Autowired
    private CNBookingService cnBookingService;
    @Autowired
    private TransitionService transitionService;

    @ApiOperation("CNBooking酒店搜索")
    @GetMapping("/hotelSearch")
    public AjaxResponseBody hotelSearch() {
        CbHotelSearchParam hotelSearchParam = new CbHotelSearchParam();
        hotelSearchParam.setCountryId("0001");
        hotelSearchParam.setHotelId("82205");
        CNResponse<CbHotelSearchData> cnResponse = cnBookingService.hotelSearch(hotelSearchParam);
        return AjaxResponseBody.success(cnResponse);
    }

    @ApiOperation("CNBooking酒店搜索返回酒店对象")
    @GetMapping("/hotelSearch/return/hotelInfo")
    public AjaxResponseBody hotelSearchReturnHotelInfo() {
        HotelInfoDTO hotelInfoDTO = transitionService.hotelSearch("0001", "82205");
        return AjaxResponseBody.success(hotelInfoDTO);
    }

    @ApiOperation("CNBooking酒店列表搜索")
    @GetMapping("/hotelListSearch")
    public AjaxResponseBody hotelListSearch() {
        CbHotelListSearchParam hotelSearchParam = getCbHotelListSearchParam();
        CNResponse<CbHotelListSearchData> cnResponse = cnBookingService.hotelListSearch(hotelSearchParam);
        return AjaxResponseBody.success(cnResponse);
    }

    @ApiOperation("CNBooking酒店列表搜索返回酒店对象")
    @GetMapping("/hotelListSearch/return/hotelInfo")
    public AjaxResponseBody hotelListSearchReturnHotelInfo() {
        CbHotelListSearchParam hotelSearchParam = getCbHotelListSearchParam();
        List<HotelInfoDTO> hotelInfoDTOS = transitionService.hotelListSearch(hotelSearchParam);
        return AjaxResponseBody.success(hotelInfoDTOS);
    }


    @ApiOperation("CNBooking预订下单前校验")
    @PostMapping("/preBookingCheck")
    public AjaxResponseBody preBookingCheck(CbPreBookingCheckParam cbPreBookingCheckParam) {
//        cbPreBookingCheckParam.setHotelId("82205");
//        cbPreBookingCheckParam.setRoomId("227408");
//        cbPreBookingCheckParam.setRateplanId("1205");
//        cbPreBookingCheckParam.setCheckIn("2019-10-19");
//        cbPreBookingCheckParam.setCheckOut("2019-10-20");
//        cbPreBookingCheckParam.setRoomCount("1");
//        cbPreBookingCheckParam.setCurrency("CNY");
//        cbPreBookingCheckParam.setOrderAmount("665");

        cbPreBookingCheckParam.setHotelId("209968");
        cbPreBookingCheckParam.setRoomId("897381");
        cbPreBookingCheckParam.setRateplanId("21797");
        cbPreBookingCheckParam.setCheckIn("2019-10-15");
        cbPreBookingCheckParam.setCheckOut("2019-10-17");
        cbPreBookingCheckParam.setRoomCount("1");
        cbPreBookingCheckParam.setCurrency("CNY");
        cbPreBookingCheckParam.setOrderAmount("420");
        CNResponse<CbPreBookingCheckData> cnResponse = cnBookingService.preBookingCheck(cbPreBookingCheckParam);
        return AjaxResponseBody.success(cnResponse);
    }

    @ApiOperation("CNBooking预订下单")
    @PostMapping("/booking")
    public AjaxResponseBody booking(@Valid CbBookingParam cbBookingParam) {
        CbBookInfo cbBookInfo = new CbBookInfo();
        cbBookInfo.setBookName("预订人姓名");
        cbBookInfo.setBookPhone("15355462520");

        CbGuestInfos cbGuestInfos = new CbGuestInfos();
        List<CbGuestInfo> list = new ArrayList<>();
        CbGuestInfo guestInfo = new CbGuestInfo();
        guestInfo.setGuestName("入住人姓名");
        guestInfo.setGuestPhone("15355462521");
//        guestInfo.setCardNum("1111");
//        guestInfo.setCardTypeId("222");
//        guestInfo.setGuestFax("12345678");
//        guestInfo.setGuestType("444");
//        list.add(guestInfo);
//        cbGuestInfos.setGuestInfos(list);
        // cbBookingParam.setGuestInfos(cbGuestInfos);
        cbBookingParam.setGuestInfo(guestInfo);

        cbBookingParam.setBookInfo(cbBookInfo);
        cbBookingParam.setHotelId("209968");
        cbBookingParam.setRoomId("897381");
        cbBookingParam.setRateplanId("21797");
        cbBookingParam.setRoomCount(1);
        cbBookingParam.setCheckIn("2019-10-15");
        cbBookingParam.setCheckOut("2019-10-16");
        cbBookingParam.setCurrency("CNY");
        cbBookingParam.setOrderAmount("210");
        cbBookingParam.setCustomerOrderId(DateUtils.nowToYMDHMSStr());

        CNResponse<CbBookingData> cnResponse = cnBookingService.booking(cbBookingParam);
        return AjaxResponseBody.success(cnResponse);
    }

    /**
     * 组装校验参数
     * @param cbBookingParam
     * @return
     */
    private CbPreBookingCheckParam getCbPreBookingCheckParam(CbBookingParam cbBookingParam){
        CbPreBookingCheckParam cbPreBookingCheckParam = new CbPreBookingCheckParam();
        cbPreBookingCheckParam.setHotelId(cbBookingParam.getHotelId());
        cbPreBookingCheckParam.setRoomId(cbBookingParam.getRoomId());
        cbPreBookingCheckParam.setRateplanId(cbBookingParam.getRateplanId());
        cbPreBookingCheckParam.setCheckIn(cbBookingParam.getCheckIn());
        cbPreBookingCheckParam.setCheckOut(cbBookingParam.getCheckOut());
        cbPreBookingCheckParam.setRoomCount(String.valueOf(cbBookingParam.getRoomCount()));
        cbPreBookingCheckParam.setCurrency(cbBookingParam.getCurrency());
        cbPreBookingCheckParam.setOrderAmount(cbBookingParam.getOrderAmount());
        return  cbPreBookingCheckParam;
    }

    @ApiOperation("CNBooking订单搜索")
    @PostMapping("/orderSearch")
    public AjaxResponseBody orderSearch(String  orderId) {
        CbOrderSearchParam cbOrderSearchParam = new CbOrderSearchParam();
        cbOrderSearchParam.setOrderId(orderId);
        CNResponse<CbOrderSearchData> cnResponse = cnBookingService.orderSearch(cbOrderSearchParam);
        return AjaxResponseBody.success(cnResponse);
    }


    @ApiOperation("价格计划查询")
    @PostMapping("/plan/search")
    public AjaxResponseBody ratePlanSearch(@Validated CbRatePlanSearchParam param,
                                           @RequestParam(value = "countryId",defaultValue = "0001") String countryId,
                                           @RequestParam(value = "hotelId", required = true) String hotelId,
                                           @RequestParam(value = "roomId", required = false) String roomId,
                                           @RequestParam(value = "ratePlanId", required = false) String ratePlanId,
                                           @RequestParam(value = "checkIn", required = true) String checkIn,
                                           @RequestParam(value = "checkOut", required = true) String checkOut) {
        param.setCountryId(countryId);
        param.setHotelId(hotelId);
        if(!StringUtils.isEmpty(roomId)){
            param.setRoomId(roomId);
        }
        if(!StringUtils.isEmpty(ratePlanId)){
            param.setRatePlanId(ratePlanId);
        }
        CbHotelListSearchStayDateRange range = new CbHotelListSearchStayDateRange();
        range.setCheckIn(checkIn);
        range.setCheckOut(checkOut);
        param.setStayDateRange(range);
        CNResponse<CbRatePlanSearchData> cnResponse = cnBookingService.ratePlanSearch(param);
        return AjaxResponseBody.success(cnResponse);
    }

    @ApiOperation("房间查询")
    @PostMapping("/room/search")
    public AjaxResponseBody roomSearch(@Validated CbRoomSearchParam roomSearchParam) {
        roomSearchParam.setCountryId("0001");
        roomSearchParam.setHotelId("82205");
        CNResponse<CbRoomSearchData> cnResponse = cnBookingService.roomSearch(roomSearchParam);
        return AjaxResponseBody.success(cnResponse);
    }

    private CbHotelListSearchParam getCbHotelListSearchParam() {
        CbHotelListSearchParam hotelSearchParam = new CbHotelListSearchParam();
        hotelSearchParam.setCityId("1201");
//        hotelSearchParam.setHotelName("西湖");
        hotelSearchParam.setLonAndLat("120.4342287385,30.237791384941");
        hotelSearchParam.setDistance("20");
        Date date = new Date();
        String checkIn = DateUtil.format(date, DatePattern.NORM_DATE_PATTERN);
        String checkOut = DateUtil.format(DateUtil.offsetDay(date, 1), DatePattern.NORM_DATE_PATTERN);
        hotelSearchParam.getStayDateRange().setCheckIn(checkIn);
        hotelSearchParam.getStayDateRange().setCheckOut(checkOut);
        hotelSearchParam.setPageSize(10);
        return hotelSearchParam;
    }

    @ApiOperation("地址查询")
    @PostMapping("/address/search")
    public AjaxResponseBody getAddressInCNBooking(CbAddressSearchParam param){

        CNResponse<CbAddressSearchData> cnResponse = cnBookingService.addressSearch(param);
        return AjaxResponseBody.success(cnResponse);
    }
}
